﻿using BLL;
using EncryptHelper;
using gfLibrary;
using IYAKASAPortal.Services;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Charge
{
    public partial class ChargeTF : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        private static List<ListItem> lstItems { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }

            if (!IsPostBack)
            {
                BindData(); 
            }
        }

        private void BindData()
        {
            lstItems = new List<ListItem>();
            CONST_DEFINE_WEB_BLL bll = new CONST_DEFINE_WEB_BLL();
            CONST_DEFINE_BLL info = new CONST_DEFINE_BLL();

            ListItem item;

            var sfxm = bll.GetModelList("CategoryID='1'");
            foreach (var v in sfxm)
            {
                item = new ListItem();
                item.Text = v.Name + "(" + v.ValueOfName + ")";
                item.Value = v.Code;
                chbFY.Items.Add(item);
            }
            
            
        }

        protected void txtHPHM_TextChanged(object sender, EventArgs e)
        {
            btnReturn.CssClass = "btnRefundEnabled";
            btnReturn.Enabled = true;
        }

        protected void rbtHPZL_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnReturn.CssClass = "btnRefundEnabled";
            btnReturn.Enabled = true;
            rbtSFJL.Items.Clear();
            chbFY.SelectedIndex = -1;
            txtFYZJE.Text = "0";
            string hphm = txtHPHM.Text.Trim();
            string hpzl = rbtHPZL.SelectedItem.Text;
            getSFLSH(hphm, hpzl);
        }

        protected void rbtSFJL_SelectedIndexChanged(object sender, EventArgs e)
        {
            gfAjaxJs.SetButtonMsg(btnReturn, "是否确认退费?");
            btnReturn.CssClass = "btnRefundEnabled";
            btnReturn.Enabled = true;
            chbFY.SelectedIndex = -1;
            txtFYZJE.Text = "0";
            string sflsh = rbtSFJL.SelectedItem.Value;
            getSFXM(sflsh);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            btnReturn.CssClass = "btnRefundEnabled";
            btnReturn.Enabled = true;
            rbtHPZL.Items.Clear();
            rbtSFJL.Items.Clear();
            chbFY.SelectedIndex = -1;
            txtFYZJE.Text = "0";

            string hphm = txtHPHM.Text.Trim();
            if (string.IsNullOrEmpty(hphm))
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "号牌号码不能为空!");
            }
            else
            {
                bool result=getHPZL(hphm);
                if (!result)
                {
                    txtMsg.Text = "没有记录!请确认号牌号码..";
                }
                else
                {
                    txtMsg.Text = "";
                }
            }
            
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            if (strHPHM.Trim().Length == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "号牌号码不能为空!");
                txtHPHM.Focus();
                return;
            }

            if (rbtHPZL.SelectedIndex < 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "没有选择号牌种类!");
                return;
            }

            if (rbtSFJL.SelectedIndex < 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "没有选择收费流水号!!");
                return;
            }

            if (chbFY.SelectedIndex < 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "没有查询到数据!!!");
                return;
            }

            CHARGE_INFO_BLL bll = new CHARGE_INFO_BLL();
            CHARGE_INFO_BLL the_bll = new CHARGE_INFO_BLL();
            CHARGE_INFO new_bill = new CHARGE_INFO();
            CHARGE_INFO old_bill = new CHARGE_INFO();
            CHARGE_INFO the_bill = new CHARGE_INFO();

            #region 退费记录
            new_bill.HPHM = txtHPHM.Text;
            new_bill.HPZL = rbtHPZL.SelectedItem.Text;
            new_bill.HPZLDH = rbtHPZL.SelectedItem.Value;

            Random rand = new Random();
            int randNum = rand.Next(1, 60);
            string key = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + randNum.ToString();
            string unique = Encrypt.MD5Encrypt(key).Substring(2, 5);
            new_bill.SFLSH = "F" + DateTime.Now.ToString("yyMMdd") + "P" + new_bill.HPZLDH + unique;
            
            new_bill.SFZL = "退费";
            new_bill.SFZLDH = "2";
            new_bill.SFZJE = (-1)*decimal.Parse(txtFYZJE.Text);

            string SFXM = "";
            for (int i = 0; i < chbFY.Items.Count; i++)
            {
                if (chbFY.Items[i].Selected)
                {
                    SFXM += chbFY.Items[i].Value+",";
                }
            }
            new_bill.SFXM = SFXM;

            new_bill.SFY = userInfo.UserName;
            new_bill.SFRQ = DateTime.Now;
            #endregion

            rbtSFJL.SelectedItem.Enabled = false;
     
            //收费记录添加退费流水号
            string sflsh = rbtSFJL.SelectedItem.Value;
            the_bill= the_bll.GetModel(sflsh);

            if (the_bill.SYBZ == "1")
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "退费失败,已使用检测服务(使用时间:"+the_bill.SYRQ+")");
                btnReturn.CssClass = "btnRefundDisabled";
                btnReturn.Enabled = false;
            }
            else
            {
                old_bill.TFLSH = new_bill.SFLSH;
                bll.Update(old_bill, sflsh);

                bool ok = bll.Insert(new_bill);
                if (ok)
                {
                    gfAjaxJs.SetAlertMsg(UpdatePanel1, "退费成功");
                    btnReturn.CssClass = "btnRefundDisabled";
                    btnReturn.Enabled = false;
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(UpdatePanel1, "退费失败");
                }
            }
            
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取号牌种类集合
        /// </summary>
        /// <param name="hphm">号牌号码</param>
        /// <returns>查询有无记录</returns>
        protected bool getHPZL(string hphm)
        {
            CHARGE_INFO_BLL sf=new CHARGE_INFO_BLL();
            ListItem item;
            bool result = false; 

            rbtHPZL.Items.Clear();
            rbtSFJL.Items.Clear();
            DataTable dtTemp = new DataTable();

            DBUtility.DbHelper.GetTable("select distinct hpzl,hpzldh from CHARGE_INFO where sfzldh='1' and tflsh is null and hphm='" + hphm + "'", ref dtTemp);

            CHARGE_INFO[] arrModel = new CHARGE_INFO[dtTemp.Rows.Count];

            for (int N = 0; N < dtTemp.Rows.Count; N++)
            {
                arrModel[N] = new CHARGE_INFO();
                arrModel[N].HPZL = dtTemp.Rows[N]["HPZL"] == DBNull.Value ? "" : dtTemp.Rows[N]["HPZL"].ToString();
                arrModel[N].HPZLDH = dtTemp.Rows[N]["HPZLDH"] == DBNull.Value ? "" : dtTemp.Rows[N]["HPZLDH"].ToString();
            }

            dtTemp.Dispose();
            
            foreach (var h in arrModel)
            {
                item = new ListItem();
                item.Text = h.HPZL;
                item.Value = h.HPZLDH;
                rbtHPZL.Items.Add(item);
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 获取收费流水号集合
        /// </summary>
        /// <param name="hphm">号牌号码</param>
        /// <param name="hpzl">号牌种类</param>
        protected void getSFLSH(string hphm, string hpzl)
        {
            CHARGE_INFO_BLL sf = new CHARGE_INFO_BLL();

            ListItem item;
            rbtSFJL.Items.Clear();
            var sflsh = sf.GetModelList("SFZLDH='1' and TFLSH IS NULL and HPHM='" + hphm + "' and HPZL='" + hpzl + "'");
            foreach (var s in sflsh)
            {
                item=new ListItem();
                item.Text = s.SFLSH;
                item.Value = s.SFLSH;
                rbtSFJL.Items.Add(item);
            }
        }

        /// <summary>
        /// 获取指定记录收费项目
        /// </summary>
        /// <param name="sflsh">收费流水号</param>
        protected void getSFXM(string sflsh)
        {
            CHARGE_INFO_BLL bll = new CHARGE_INFO_BLL();
            var result = bll.GetModel(sflsh);
            string[] sfxm = result.SFXM.Split(',');
            chbFY.SelectedIndex = -1;
            for (int i = 0; i < chbFY.Items.Count; i++)
            {
                for (int j = 0; j < sfxm.Length; j++)
                {
                    if (chbFY.Items[i].Value == sfxm[j])
                    {
                        chbFY.Items[i].Selected = true;
                    }
                }
            }
            txtFYZJE.Text = result.SFZJE.ToString();
        }

    }
}